#!/opt/exp/bin/tclsh
#  -*-Tcl-*- for (X)emacs ##


### USAGE:
#   ln -s /home/ggreif/aL4nin/purgatory/clear-c DIR/clear-c
#   ln -s /home/ggreif/aL4nin/purgatory/clear-c DIR/clear-m
#   ln -s /home/ggreif/aL4nin/purgatory/clear-c DIR/clear-a
#   ln -s /home/ggreif/aL4nin/purgatory/clear-c DIR/clear-l
#   rm ./Version.o makefile ; gmake -f Makefile EXEC=omega \
#        GHC="ghc -O -pgmc clear-c -pgmm clear-m -pgma clear-a -pgml clear-l"

## SEE: http://www.haskell.org/ghc/docs/latest/html/users_guide/options-phases.html

puts "\#\#\# argv0 = $argv0"

set mangler "/home/ggreif/lib/ghc-6.4.1/ghc-asm"

proc find-o { } {
    global argv

    if [llength $argv]==2 {
	puts stderr "OUT2: [lindex $argv 1]\n"
	return [lindex $argv 1]
    }

    set flag ""

    foreach i $argv {
	puts stderr "\#\#\#\#\#\#\#\# $i $flag\n"
	if [string equal "" $flag] {
	puts stderr "WOOOO $i\n"
	    if [string equal $i "-o"] {
		set flag $i
	puts stderr "POOOO $flag\n"
	    }
	} else {
	puts stderr "HEY $flag    ---> $i\n"
	    switch -exact -- $flag {
		-o { set out $i }
	    }
	    set flag ""
	}
    }

	puts stderr "OUT: $out\n"
    return $out
}

proc findObjects { } {
    global argv
    set objs {}

    foreach i $argv {
	switch -glob -- $i {
	    "*\\.o" {lappend objs "$i"}
	    default {}
	}
    }

    return $objs
}

global tmpdir
set tmpdir "/home/ggreif/.UCSITmpDir/clear-tmp"

switch -glob "$argv0" {
    *clear-c {
	puts "==========="
	exec mkdir -p $tmpdir
	set outF "[find-o]"
	set inF "[lindex $argv 2]"
	puts "===========$outF   $inF"
	set subst1 "[regsub -all $inF $argv \$^]"
	set subst2 "[regsub -all $outF $subst1 \$@]"
	puts "===========>>>>>> gcc $subst2"
	exec rm -f "${tmpdir}/x.hc"
	exec mv "$inF" "${tmpdir}/x.hc"
	set rul [open "${tmpdir}/x.hc.rule" w]
	puts $rul "\tgcc $subst2"
	close $rul
	exit
    }
    *clear-m {
	puts "=====clear-m======"
	set outF "[lindex $argv 0]"
	set inF "[lindex $argv 1]"
	puts "===========$outF   $inF"
	exit
    }
    *clear-a {
	puts "=====clear-a======"
	set outF "[find-o]"
	set subst1 "[regsub \\.o $outF .hc]"
	set subst2 "[regsub \\.o $outF .raw_s]"
	set subst3 "[regsub \\.o $outF .s]"

	set inF "[lindex $argv 2]"
	set substArgv "[regsub -all $inF $argv \$^]"

	exec rm -f "$subst1"
	exec mv ${tmpdir}/x.hc "$subst1"
	#set inF "[lindex $argv 1]"
	puts "===========$outF $subst1  $argv"
	set mak [open "makefile" a]
	puts $mak "$subst2: $subst1\n[exec cat ${tmpdir}/x.hc.rule]"
	
	puts $mak "%.s: %.raw_s\n\t$mangler $^ $@"
	puts $mak "$outF: $subst3\n\tgcc $substArgv"
	close $mak
	exit
    }
    *clear-l {
	puts "=====clear-l======"
	set mak [open "makefile" a]
	puts $mak "[find-o]: [findObjects]"
	puts $mak "\tgcc $argv"
	# puts $mak "OUTFILE is [find-o]"
	close $mak
	exit
    }

    * {puts "\#\#\# argv0 = $argv0 -------------------------------------------------------TOUCHING!!!"}
}

#set mak [open "makefile" a]
#puts $mak "$argv"
#puts $mak "OUTFILE is [find-o]"
#close $mak

exec touch ./Version.hs
exec touch ./Toplevel.hs

